﻿using System;
using System.Windows;
using System.Windows.Controls;
using Bridge.UI;
using Pathfinder.Base.Constructs;

namespace Pathfinder.Base.UI
{
    /// <summary>
    /// Interaction logic for EditAttack.xaml
    /// </summary>
    public partial class EditAttack : UserControl
    {
        private enum AttackSection
        {
            None = 0,
            MeleeRanged = 1,
            Natural = 2,
            PrimarySecondary = 3,
            TwoHanded = 4,
            AttackName = 5,
            ToHit = 6,
            Critical = 7,
            Damage = 8,
            All = 10
        }

        /// <summary>
        /// Creates an EditAttack UserControl for a previously made attack.
        /// </summary>
        /// <param name="attack">Attack to copy.</param>
        /// <exception cref="ArgumentNullException">Thrown if attack is null.</exception>
        public EditAttack(Attack attack)
            : this()
        {
            if (attack == null) throw new ArgumentNullException("attack");

            mAttack = new Attack(attack);
            mMaxAttackSection = AttackSection.All;

            UpdateSections();
            UpdateMeleeRanged();
            UpdateNatural();
            UpdatePrimarySecondary();
            UpdateTwoHanded();
            UpdateAttackName();
            UpdateToHit();
            UpdateCritical();
            UpdateDamage();
        }

        /// <summary>
        /// Creates an EditAttack UserControl for a new attack.
        /// </summary>
        /// <param name="pathfinderCharacter">Character to use as a base.</param>
        /// <exception cref="ArgumentNullException">Thrown if pathfinderCharacter is null.</exception>
        public EditAttack(PathfinderCharacter pathfinderCharacter)
            : this()
        {
            if (pathfinderCharacter == null) throw new ArgumentNullException("pathfinderCharacter");

            mAttack = new Attack(pathfinderCharacter);
            mMaxAttackSection = AttackSection.MeleeRanged;

            UpdateSections();
        }

        /// <summary>
        /// Gets the result of the popup.
        /// </summary>
        /// <value>Null if nothing has been reported yet; true if user clicked ok, false if user clicked Cancel or closed the window.</value>
        public bool? Result
        {
            get { return mResult; }
        }

        /// <summary>
        /// Gets the Attack result.
        /// </summary>
        public Attack Attack
        {
            get { return mAttack; }
        }

        private EditAttack()
        {
            InitializeComponent();

            mActiveAttackSection = AttackSection.MeleeRanged;
            UpdateSections();

        }

        private Attack mAttack;
        private bool mIsUpdating = true;
        private bool? mResult = null;

        private AttackSection mActiveAttackSection = AttackSection.None;
        private AttackSection mMaxAttackSection = AttackSection.All;

        private void UpdateSections()
        {
            buttonIsMeleeRanged.IsEnabled = mMaxAttackSection >= AttackSection.MeleeRanged;
            buttonIsNatural.IsEnabled = mMaxAttackSection >= AttackSection.Natural;
            buttonIsSecondary.IsEnabled = mMaxAttackSection >= AttackSection.PrimarySecondary;
            buttonTwoHanded.IsEnabled = mMaxAttackSection >= AttackSection.TwoHanded;
            buttonAttackName.IsEnabled = mMaxAttackSection >= AttackSection.AttackName;
            buttonToHit.IsEnabled = mMaxAttackSection >= AttackSection.ToHit;
            buttonCritical.IsEnabled = mMaxAttackSection >= AttackSection.Critical;
            buttonDamage.IsEnabled = mMaxAttackSection >= AttackSection.Damage;

            buttonIsMeleeRanged.Tag = mActiveAttackSection == AttackSection.MeleeRanged ? "Yes" : "No";
            buttonIsNatural.Tag = mActiveAttackSection == AttackSection.Natural ? "Yes" : "No";
            buttonIsSecondary.Tag = mActiveAttackSection == AttackSection.PrimarySecondary ? "Yes" : "No";
            buttonTwoHanded.Tag = mActiveAttackSection == AttackSection.TwoHanded ? "Yes" : "No";
            buttonAttackName.Tag = mActiveAttackSection == AttackSection.AttackName ? "Yes" : "No";
            buttonToHit.Tag = mActiveAttackSection == AttackSection.ToHit ? "Yes" : "No";
            buttonCritical.Tag = mActiveAttackSection == AttackSection.Critical ? "Yes" : "No";
            buttonDamage.Tag = mActiveAttackSection == AttackSection.Damage ? "Yes" : "No";

            borderRangedValue.Visibility = mActiveAttackSection == AttackSection.MeleeRanged ? Visibility.Visible : Visibility.Collapsed;
            borderIsNaturalValue.Visibility = mActiveAttackSection == AttackSection.Natural ? Visibility.Visible : Visibility.Collapsed;
            borderIsSecondaryValue.Visibility = mActiveAttackSection == AttackSection.PrimarySecondary ? Visibility.Visible : Visibility.Collapsed;
            borderTwoHandedValue.Visibility = mActiveAttackSection == AttackSection.TwoHanded ? Visibility.Visible : Visibility.Collapsed;
            borderAttackNameValue.Visibility = mActiveAttackSection == AttackSection.AttackName ? Visibility.Visible : Visibility.Collapsed;
            borderToHitValue.Visibility = mActiveAttackSection == AttackSection.ToHit ? Visibility.Visible : Visibility.Collapsed;
            borderCriticalValue.Visibility = mActiveAttackSection == AttackSection.Critical ? Visibility.Visible : Visibility.Collapsed;
            borderDamageValue.Visibility = mActiveAttackSection == AttackSection.Damage ? Visibility.Visible : Visibility.Collapsed;
        }

        private void UpdateMeleeRanged()
        {
            buttonMelee.IsEnabled = mAttack.IsRanged;
            buttonRanged.IsEnabled = !mAttack.IsRanged;
            textBlockMeleeRanged.Tag = mAttack.IsRanged ? "Right" : "Left";
        }

        private void UpdateNatural()
        {
            buttonIsNaturalNo.IsEnabled = mAttack.IsNatural;
            buttonIsNaturalYes.IsEnabled = !mAttack.IsNatural;
            textBlockIsNatural.Tag = mAttack.IsNatural ? "Left" : "Right";
            stackPanelNumberAttacks.Visibility = mAttack.IsNatural ? Visibility.Visible : Visibility.Collapsed;
            textBoxIsNaturalNumber.Text = mAttack.NumberAttacks.ToString();
        }

        private void UpdatePrimarySecondary()
        {
            buttonNoSecondary.IsEnabled = mAttack.IsSecondary;
            buttonYesSecondary.IsEnabled = !mAttack.IsSecondary;
            textBlockSecondary.Tag = mAttack.IsSecondary ? "Left" : "Right";
        }

        private void UpdateTwoHanded()
        {
            buttonNoTwoHanded.IsEnabled = mAttack.IsTwoWeaponFighting;
            buttonYesTwoHanded.IsEnabled = !mAttack.IsTwoWeaponFighting;
            textBlockTwoHanded.Tag = mAttack.IsTwoWeaponFighting ? "Right" : "Left";

            stackPanelMainHand.Visibility = mAttack.IsTwoWeaponFighting ? Visibility.Visible : Visibility.Collapsed;
            stackPanelTwoWeaponDetect.Visibility = mAttack.IsTwoWeaponFighting ? Visibility.Visible : Visibility.Collapsed;
            stackPanelLightOff.Visibility = mAttack.IsTwoWeaponFighting ? Visibility.Visible : Visibility.Collapsed;

            buttonTwoHandedMainHand.IsEnabled = !mAttack.IsMainHand;
            buttonTwoHandedOffHand.IsEnabled = mAttack.IsMainHand;
            textBlockTwoHandedMainOff.Tag = mAttack.IsMainHand ? "Left" : "Right";

            buttonTwoHandedLightOffYes.IsEnabled = !mAttack.IsOffhandLight;
            buttonTwoHandedLightOffNo.IsEnabled = mAttack.IsOffhandLight;
            textBlockTwoHandedLight.Tag = mAttack.IsOffhandLight ? "Left" : "Right";

            runGreaterTwoWeaponDetect.Text = mAttack.HasGreaterTwoWeaponFighting ? string.Empty : "Not";
            runImprovedTwoWeaponDetect.Text = mAttack.HasImprovedTwoWeaponFighting ? string.Empty : "Not";
            runMultiattackDetect.Text = mAttack.HasMultiattack ? string.Empty : "Not";
            runMultiweaponDetect.Text = mAttack.HasMultiweaponFighting ? string.Empty : "Not";
            runTwoWeaponDetect.Text = mAttack.HasTwoWeaponFighting ? string.Empty : "Not";
        }

        private void UpdateAttackName()
        {
            textBoxAttackName.Text = mAttack.AttackString;
        }

        private void UpdateToHit()
        {
            textBlockToHitBAB.Text = "+" + mAttack.BAB;
            checkBoxToHitFinessable.IsEnabled = mAttack.HasWeaponFinesse && !mAttack.IsRanged;
            checkBoxToHitFinessable.IsChecked = mAttack.IsFinessable && mAttack.HasWeaponFinesse && !mAttack.IsRanged;
            checkBoxToHitFinessable.Content = mAttack.IsRanged ? "Ranged" : (mAttack.HasWeaponFinesse ? string.Empty : "NOT DETECTED");
            textBlockToHitStrBonusTitle.Text = ((mAttack.IsFinessable && mAttack.HasWeaponFinesse) || mAttack.IsRanged) ? "Dexterity Bonus:" : "Strength Bonus:";
            textBlockToHitStrBonus.Text = ((((mAttack.IsFinessable && mAttack.HasWeaponFinesse) || mAttack.IsRanged) ? mAttack.DexterityBonus : mAttack.StrengthBonus) >= 0 ? "+" : string.Empty) + (((mAttack.IsFinessable && mAttack.HasWeaponFinesse) || mAttack.IsRanged) ? mAttack.DexterityBonus.ToString() : mAttack.StrengthBonus.ToString());
            textBlockToHitExtra.Text = mAttack.HitBonus.ToString();

            textBlockToHitTotalBonus.Text = mAttack.ToHitBonusString;
        }

        private void UpdateCritical()
        {
            radioButtonCriticalMultiplier2.IsChecked = mAttack.CritMultiplier == 2;
            radioButtonCriticalMultiplier3.IsChecked = mAttack.CritMultiplier == 3;
            radioButtonCriticalMultiplier4.IsChecked = mAttack.CritMultiplier == 4;
            radioButtonCriticalRange15.IsChecked = mAttack.CritRange == 15;
            radioButtonCriticalRange16.IsChecked = mAttack.CritRange == 16;
            radioButtonCriticalRange17.IsChecked = mAttack.CritRange == 17;
            radioButtonCriticalRange18.IsChecked = mAttack.CritRange == 18;
            radioButtonCriticalRange19.IsChecked = mAttack.CritRange == 19;
            radioButtonCriticalRange20.IsChecked = mAttack.CritRange == 20;
        }

        private void UpdateDamage()
        {
            textBlockDamageStrBonus.Text = (mAttack.StrengthBonus >= 0 ? "+" : string.Empty) + mAttack.StrengthBonus.ToString();
            checkBoxDamageTwoHands.IsChecked = mAttack.StrBonus150 && !mAttack.IsTwoWeaponFighting;
            checkBoxDamageTwoHands.IsEnabled = !mAttack.IsTwoWeaponFighting;
            checkBoxDamageTwoHands.Content = mAttack.IsTwoWeaponFighting ? "Two-Weapon Fighting" : string.Empty;
            int strBonus = Math.Max(0, mAttack.StrengthBonus);
            textBlockDamageBonusStrBonus.Text = (mAttack.StrBonus150 && !mAttack.IsTwoWeaponFighting ? Math.Floor((double)strBonus / 2) : 0).ToString();
            textBoxDamageDiceNumber.Text = mAttack.BaseDamage.DiceAmount.ToString();
            comboBoxDamageDice.ItemsSource = mAttack.BaseDamage.PossibleDamageDiceFace;
            comboBoxDamageDice.SelectedItem = mAttack.BaseDamage.DiceFace;
            textBoxDamageNormalDamage.Text = mAttack.BaseDamage.FlatAmountString;
            textBlockDamageNormalDamage.Text = mAttack.DamageString;
            textBlockDamageDamageOnCrit.Text = mAttack.CritDamageString;
            checkBoxUnlimitedMaxStrength.IsChecked = mAttack.MaxStrength < 0;
            textBoxMaxStrength.IsEnabled = mAttack.MaxStrength >= 0;
            textBoxMaxStrength.Text = mAttack.MaxStrength >= 0 ? mAttack.MaxStrength.ToString() : string.Empty;
        }

        private void buttonOK_Click(object sender, RoutedEventArgs e)
        {
            mResult = true;
            CustomPopup customPopup = (CustomPopup)Tag;
            customPopup.Close();
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            mResult = false;
            CustomPopup customPopup = (CustomPopup)Tag;
            customPopup.Close();
        }

        private void buttonIsMeleeRanged_Click(object sender, RoutedEventArgs e)
        {
            mActiveAttackSection = AttackSection.MeleeRanged;
            UpdateSections();
        }

        private void buttonTwoHanded_Click(object sender, RoutedEventArgs e)
        {
            mActiveAttackSection = AttackSection.TwoHanded;
            UpdateSections();
        }

        private void buttonAttackName_Click(object sender, RoutedEventArgs e)
        {
            mActiveAttackSection = AttackSection.AttackName;
            UpdateSections();
        }

        private void buttonIsSecondary_Click(object sender, RoutedEventArgs e)
        {
            mActiveAttackSection = AttackSection.PrimarySecondary;
            UpdateSections();
        }

        private void buttonToHit_Click(object sender, RoutedEventArgs e)
        {
            mActiveAttackSection = AttackSection.ToHit;
            UpdateSections();
        }

        private void buttonCritical_Click(object sender, RoutedEventArgs e)
        {
            mActiveAttackSection = AttackSection.Critical;
            UpdateSections();
        }

        private void buttonMelee_Click(object sender, RoutedEventArgs e)
        {
            mIsUpdating = true;

            mAttack.IsRanged = false;
            UpdateMeleeRanged();
            UpdateToHit();
            UpdateDamage();
            if (mMaxAttackSection == AttackSection.MeleeRanged)
            {
                mMaxAttackSection = AttackSection.Natural;
            }
            UpdateSections();

            mIsUpdating = false;
        }

        private void buttonRanged_Click(object sender, RoutedEventArgs e)
        {
            mIsUpdating = true;

            mAttack.IsRanged = true;
            UpdateMeleeRanged();
            UpdateToHit();
            UpdateDamage();
            if (mMaxAttackSection == AttackSection.MeleeRanged)
            {
                mMaxAttackSection = AttackSection.Natural;
            }
            UpdateSections();

            mIsUpdating = false;
        }

        private void buttonNoSecondary_Click(object sender, RoutedEventArgs e)
        {
            mIsUpdating = true;

            mAttack.IsSecondary = false;
            UpdatePrimarySecondary();
            UpdateToHit();
            UpdateDamage();
            if (mMaxAttackSection == AttackSection.PrimarySecondary)
            {
                mMaxAttackSection = AttackSection.TwoHanded;
            }
            UpdateSections();

            mIsUpdating = false;
        }

        private void buttonYesSecondary_Click(object sender, RoutedEventArgs e)
        {
            mIsUpdating = true;

            mAttack.IsSecondary = true;
            UpdatePrimarySecondary();
            UpdateToHit();
            UpdateDamage();
            if (mMaxAttackSection == AttackSection.PrimarySecondary)
            {
                mMaxAttackSection = AttackSection.TwoHanded;
            }
            UpdateSections();

            mIsUpdating = false;
        }

        private void buttonNoTwoHanded_Click(object sender, RoutedEventArgs e)
        {
            mIsUpdating = true;

            mAttack.IsTwoWeaponFighting = false;
            borderTwoHandedValue.Visibility = Visibility.Collapsed;
            UpdateTwoHanded();
            UpdateToHit();
            UpdateDamage();
            if (mMaxAttackSection == AttackSection.TwoHanded)
            {
                mMaxAttackSection = AttackSection.AttackName;
            }
            UpdateSections();

            mIsUpdating = false;
        }

        private void buttonYesTwoHanded_Click(object sender, RoutedEventArgs e)
        {
            mIsUpdating = true;

            mAttack.IsTwoWeaponFighting = true;
            borderTwoHandedValue.Visibility = Visibility.Visible;
            UpdateTwoHanded();
            UpdateToHit();
            UpdateDamage();
            if (mMaxAttackSection == AttackSection.TwoHanded)
            {
                mMaxAttackSection = AttackSection.AttackName;
            }
            UpdateSections();

            mIsUpdating = false;
        }

        private void buttonTwoHandedMainHand_Click(object sender, RoutedEventArgs e)
        {
            mIsUpdating = true;

            mAttack.IsMainHand = true;
            UpdateTwoHanded();
            UpdateToHit();
            UpdateDamage();
            UpdateSections();

            mIsUpdating = false;
        }

        private void buttonTwoHandedOffHand_Click(object sender, RoutedEventArgs e)
        {
            mIsUpdating = true;

            mAttack.IsMainHand = false;
            UpdateTwoHanded();
            UpdateToHit();
            UpdateDamage();
            UpdateSections();

            mIsUpdating = false;
        }

        private void buttonTwoHandedLightOffYes_Click(object sender, RoutedEventArgs e)
        {
            mIsUpdating = true;

            mAttack.IsOffhandLight = true;
            UpdateTwoHanded();
            UpdateToHit();
            UpdateDamage();
            UpdateSections();

            mIsUpdating = false;
        }

        private void buttonTwoHandedLightOffNo_Click(object sender, RoutedEventArgs e)
        {
            mIsUpdating = true;

            mAttack.IsOffhandLight = false;
            UpdateTwoHanded();
            UpdateToHit();
            UpdateDamage();
            UpdateSections();

            mIsUpdating = false;
        }

        private void textBoxAttackName_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (mIsUpdating) return;

            mIsUpdating = true;

            mAttack.AttackString = textBoxAttackName.Text;
            UpdateToHit();
            UpdateDamage();
            if (mMaxAttackSection == AttackSection.AttackName)
            {
                mMaxAttackSection = AttackSection.ToHit;
            }
            UpdateSections();

            mIsUpdating = false;
        }

        private void checkBoxToHitFinessable_Checked(object sender, RoutedEventArgs e)
        {
            if (mIsUpdating) return;

            mIsUpdating = true;

            mAttack.IsFinessable = true;
            UpdateToHit();
            UpdateDamage();
            if (mMaxAttackSection == AttackSection.ToHit)
            {
                mMaxAttackSection = AttackSection.Critical;
            }
            UpdateSections();

            mIsUpdating = false;
        }

        private void checkBoxToHitFinessable_Unchecked(object sender, RoutedEventArgs e)
        {
            if (mIsUpdating) return;

            mIsUpdating = true;

            mAttack.IsFinessable = true;
            UpdateToHit();
            UpdateDamage();
            if (mMaxAttackSection == AttackSection.ToHit)
            {
                mMaxAttackSection = AttackSection.Critical;
            }
            UpdateSections();

            mIsUpdating = false;
        }

        private void textBlockToHitExtra_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (mIsUpdating) return;

            int extraToHitBonus = 0;

            if (int.TryParse(textBlockToHitExtra.Text, out extraToHitBonus))
            {
                mIsUpdating = true;

                mAttack.HitBonus = extraToHitBonus;
                UpdateToHit();
                UpdateDamage();
                if (mMaxAttackSection == AttackSection.ToHit)
                {
                    mMaxAttackSection = AttackSection.Critical;
                }
                UpdateSections();

                mIsUpdating = false;
            }
        }

        private void checkBoxDamageTwoHands_Checked(object sender, RoutedEventArgs e)
        {
            if (mIsUpdating) return;

            mIsUpdating = true;

            mAttack.StrBonus150 = true;
            UpdateToHit();
            UpdateDamage();
            if (mMaxAttackSection == AttackSection.Damage)
            {
                mMaxAttackSection = AttackSection.All;
            }
            UpdateSections();

            mIsUpdating = false;
        }

        private void checkBoxDamageTwoHands_Unchecked(object sender, RoutedEventArgs e)
        {
            if (mIsUpdating) return;

            mIsUpdating = true;

            mAttack.StrBonus150 = false;
            UpdateToHit();
            UpdateDamage();
            if (mMaxAttackSection == AttackSection.Damage)
            {
                mMaxAttackSection = AttackSection.All;
            }
            UpdateSections();

            mIsUpdating = false;
        }

        private void textBoxDamageDiceNumber_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (mIsUpdating) return;

            int intDamage = 0;
            if (int.TryParse(textBoxDamageDiceNumber.Text, out intDamage))
            {
                mIsUpdating = true;

                mAttack.BaseDamage.DiceAmount = intDamage;
                UpdateDamage();
                if (mMaxAttackSection == AttackSection.Damage)
                {
                    mMaxAttackSection = AttackSection.All;
                }
                UpdateSections();

                mIsUpdating = false;
            }
        }

        private void comboBoxDamageDice_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (mIsUpdating) return;

            int damageDice = (int)comboBoxDamageDice.SelectedItem;
            mIsUpdating = true;

            mAttack.BaseDamage.DiceFace = damageDice;
            UpdateDamage();
            if (mMaxAttackSection == AttackSection.Damage)
            {
                mMaxAttackSection = AttackSection.All;
            }
            UpdateSections();

            mIsUpdating = false;
        }

        private void textBoxDamageNormalDamage_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (mIsUpdating) return;

            int intDamage = 0;
            if (int.TryParse(textBoxDamageNormalDamage.Text, out intDamage))
            {
                mIsUpdating = true;

                mAttack.BaseDamage.FlatAmount = intDamage;
                UpdateDamage();
                if (mMaxAttackSection == AttackSection.Damage)
                {
                    mMaxAttackSection = AttackSection.All;
                }
                UpdateSections();

                mIsUpdating = false;
            }
        }

        private void radioButtonCriticalRange_Checked(object sender, RoutedEventArgs e)
        {
            if (mIsUpdating) return;

            RadioButton radioButton = (RadioButton)sender;
            string criticalMultiplierString = radioButton.Content.ToString();
            int criticalMultiplier = 20;
            switch (criticalMultiplierString)
            {
                case "20": criticalMultiplier = 20; break;
                case "19-20": criticalMultiplier = 19; break;
                case "18-20": criticalMultiplier = 18; break;
                case "17-20": criticalMultiplier = 17; break;
                case "16-20": criticalMultiplier = 16; break;
                case "15-20": criticalMultiplier = 15; break;
            }
            mIsUpdating = true;

            mAttack.CritRange = criticalMultiplier;
            UpdateDamage();
            if (mMaxAttackSection == AttackSection.Critical)
            {
                mMaxAttackSection = AttackSection.Damage;
            }
            UpdateSections();

            mIsUpdating = false;
        }

        private void radioButtonCriticalMultiplier_Checked(object sender, RoutedEventArgs e)
        {
            if (mIsUpdating) return;

            RadioButton radioButton = (RadioButton)sender;
            string criticalMultiplierString = radioButton.Content.ToString();
            int criticalMultiplier = 2;
            switch (criticalMultiplierString)
            {
                case "x2": criticalMultiplier = 2; break;
                case "x3": criticalMultiplier = 3; break;
                case "x4": criticalMultiplier = 4; break;
            }
            mIsUpdating = true;

            mAttack.CritMultiplier = criticalMultiplier;
            UpdateDamage();
            if (mMaxAttackSection == AttackSection.Critical)
            {
                mMaxAttackSection = AttackSection.Damage;
            }
            UpdateSections();

            mIsUpdating = false;
        }

        private void buttonIsNaturalYes_Click(object sender, RoutedEventArgs e)
        {
            mIsUpdating = true;

            mAttack.IsNatural = true;
            UpdateNatural();
            UpdateToHit();
            UpdateDamage();
            if (mMaxAttackSection == AttackSection.Natural)
            {
                mMaxAttackSection = AttackSection.PrimarySecondary;
            }
            stackPanelNumberAttacks.Visibility = Visibility.Visible;
            UpdateSections();

            mIsUpdating = false;
        }

        private void buttonIsNaturalNo_Click(object sender, RoutedEventArgs e)
        {
            mIsUpdating = true;

            mAttack.IsNatural = false;
            UpdateNatural();
            UpdateToHit();
            UpdateDamage();
            if (mMaxAttackSection == AttackSection.Natural)
            {
                mMaxAttackSection = AttackSection.PrimarySecondary;
            }
            stackPanelNumberAttacks.Visibility = Visibility.Collapsed;
            UpdateSections();

            mIsUpdating = false;
        }


        private void buttonIsNatural_Click(object sender, RoutedEventArgs e)
        {
            mActiveAttackSection = AttackSection.Natural;
            UpdateSections();
        }

        private void textBoxIsNaturalNumber_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (mIsUpdating) return;

            int naturalAttacks = 1;
            if (int.TryParse(textBoxIsNaturalNumber.Text, out naturalAttacks))
            {
                mAttack.NumberAttacks = naturalAttacks;
                UpdateToHit();
                UpdateDamage();
            }
        }

        private void buttonAddDamage_Click(object sender, RoutedEventArgs e)
        {
            mAttack.AddDamage(new Damage());
        }

        private void buttonRemoveDamage_Click(object sender, RoutedEventArgs e)
        {
            if (listBoxDamages.SelectedItem != null)
            {
                Damage damage = (Damage)listBoxDamages.SelectedItem;
                mAttack.RemoveDamage(damage);
                listBoxDamages.ItemsSource = mAttack.ExtraDamages;
            }
        }

        private void buttonDamage_Click(object sender, RoutedEventArgs e)
        {
            mActiveAttackSection = AttackSection.Damage;
            UpdateSections();
        }

        private void textBoxMaxStrength_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (mIsUpdating) return;

            int maxStrength = -1;
            if (int.TryParse(textBoxMaxStrength.Text, out maxStrength))
            {
                mIsUpdating = true;
                mAttack.MaxStrength = maxStrength;
                UpdateDamage();
                mIsUpdating = false;
            }
        }

        private void checkBoxUnlimitedMaxStrength_Checked(object sender, RoutedEventArgs e)
        {
            if (mIsUpdating) return;

            mIsUpdating = true;
            mAttack.MaxStrength = -1;
            UpdateDamage();
            mIsUpdating = false;
        }

        private void checkBoxUnlimitedMaxStrength_Unchecked(object sender, RoutedEventArgs e)
        {
            if (mIsUpdating) return;

            mIsUpdating = true;
            mAttack.MaxStrength = 0;
            UpdateDamage();
            mIsUpdating = false;
        }
    }
}
